//
//  WBComposeViewController.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/8.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit

class WBComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //MARK: - 设置视图
    private func setupUI(){
        setupNav()
        // 添加控件
        view.addSubview(textView)
        textView.addSubview(pictureView)
        view.addSubview(bottomView)
        // 添加约束
        textView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(view)
        }
        pictureView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: SCREENW - 20, height: SCREENW - 20))
            make.top.equalTo(textView).offset(100)
            make.centerX.equalTo(textView)
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalTo(view)
            make.height.equalTo(35)
        }
        // 02 闭包的实例化
        bottomView.closure = {(type)->() in
            // 04 闭包回调
            
            switch type {
            case .picture:
                print("图片")
            case .mention:
                print("@")
            case .trend:
                print("#")
            case .emoticon:
                print("笑脸")
            case .add:
                print("+")
            }
        }



    }
    //MARK: - 设置导航
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: nil, title: "取消", target: self, action: #selector(cancelClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        // 默认不能点击
        navigationItem.rightBarButtonItem?.isEnabled = false
        
        navigationItem.titleView = titleView
    }
    //MARK: - 懒加载控件
    private lazy var sendButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(sendClick), for: UIControlEvents.touchUpInside)
        button.setBackgroundImage(UIImage(named:"common_button_orange"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"common_button_orange_highlighted"), for: UIControlState.highlighted)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: UIControlState.disabled)
        button.setTitle("发送", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitleColor(UIColor.darkGray, for: UIControlState.disabled)
        button.titleLabel?.font = UIFont.systemFont(ofSize: NormalFontSize)
        button.frame.size = CGSize(width: 50, height: 35)
        return button
    }()
//    // titleView------------------
    private lazy var titleView: UILabel = {
        // 获取名字
        let name = WBOAuthViewModel.share.userAccountModel?.screen_name ?? ""
        // 最终要显示的文字
        let str = "发微博\n\(name)"
        // 范围
        let rang = (str as NSString).range(of: name)
        // 通过字符串获得富文本文
        let attr = NSMutableAttributedString(string: str)
        // 设置富文本属性
        attr.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: ThemeColor], range: rang)
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.attributedText = attr
        lab.textAlignment = .center
        lab.sizeToFit()
        return lab
    }()
    // 自定义textView---------
    private lazy var textView: WBComposeTextView = {
       let view = WBComposeTextView()
        view.placeholder = "黑马哈哈哈"
        view.delegate = self
        view.font = UIFont.systemFont(ofSize: 16)
        view.alwaysBounceVertical = true
        return view
    }()
    // 底部视图
    private lazy var bottomView: WBComposeBottomView = WBComposeBottomView()
    // 自定义配图
    private lazy var pictureView: WBComposePictureView = WBComposePictureView()
   
}
//MARK: - 代理发放
extension WBComposeViewController: UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {
        navigationItem.rightBarButtonItem?.isEnabled = textView.hasText
    }
}

//MARK: - 监听事件
extension WBComposeViewController{
    @objc fileprivate func cancelClick(){
        dismiss(animated: true, completion: nil)
    }
    
    @objc fileprivate func sendClick(){
        print("发送按钮点击")
    }
}
